package edu.nf.vrs.vaccination.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import edu.nf.vrs.search.dto.NeedleNumSearchDTO;
import edu.nf.vrs.search.service.NeedleNumSearchService;
import edu.nf.vrs.vaccination.config.RabbitMQConfig;
import edu.nf.vrs.vaccination.dao.NeedleNumDao;
import edu.nf.vrs.vaccination.dto.NeedleNumDTO;
import edu.nf.vrs.vaccination.entity.Message;
import edu.nf.vrs.vaccination.entity.NeedleNum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author: @Wai
 * Date: 2022-3-30
 */
@Service
@Slf4j
public class NeedleSearchService {

    @DubboReference(interfaceClass = NeedleNumSearchService.class, version = "1.0.0",check = false)
    private NeedleNumSearchService service;
    @Autowired
    private NeedleNumDao needleNumDao;

    private final String tableName = "needle_num_info";

    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
    public void receiveMessage(@Payload String json, @Headers Map<String, Object> headers, Channel channel) throws JsonProcessingException {
        log.info("canal ==>"+json);
        Message message = new ObjectMapper().readValue(json, Message.class);
        if (tableName.equals(message.getTable())) {
            Integer[] ids = new Integer[message.getData().size()];
            for (int i = 0; i < ids.length; i++) {
                ids[i] = message.getData().get(i).getNid();
            }
            List<NeedleNum> needleNums = needleNumDao.listNeedleNumById(ids);
            ArrayList<NeedleNumSearchDTO> list = new ArrayList<>();
            needleNums.forEach(needleNum -> {
                NeedleNumSearchDTO numSearchDTO = BeanUtil.copyProperties(needleNum, NeedleNumSearchDTO.class);
                numSearchDTO.setInoculateDate(DateUtil.formatDate(needleNum.getInoculateDate()));
                numSearchDTO.setAreaName(needleNum.getVaccination().getArea().getAreaName());
                numSearchDTO.setAddress(needleNum.getVaccination().getAddress());
                numSearchDTO.setTel(needleNum.getVaccination().getTel());
                numSearchDTO.setDetailed(needleNum.getVaccination().getDetailed());
                list.add(numSearchDTO);
            });
            String needleJson = new ObjectMapper().writeValueAsString(list);
            if ("INSERT".equals(message.getType())) service.add(needleJson);
            else if ("UPDATE".equals(message.getType())) service.update(needleJson);
            else if ("DELETE".equals(message.getType())) service.del(ids);
            log.info(message.getType() + "===>" + message.getTable());
        }
    }
}
